package com.fantasychong.myandroidapplication.recyclerView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.fantasychong.myandroidapplication.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 主页的界面
 * Created by Fantasychong
 * Date: 2023/1/28
 */
public class RecyclerViewActivity extends AppCompatActivity implements View.OnClickListener {

    private androidx.recyclerview.widget.RecyclerView rv;
    private Button addBtn;
    private Button removeBtn;
    private SmartRefreshLayout refreshLayout;
    private List<RecyclerViewBean> list;
    private RecyclerViewAdapter adapter;
    private androidx.recyclerview.widget.RecyclerView.ItemDecoration decoration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);

        initViews(); //配置组件
    }

    /**
     * 配置组件
     */
    private void initViews() {
        rv = findViewById(R.id.main_rv);
        addBtn = findViewById(R.id.main_addBtn);
        removeBtn = findViewById(R.id.main_removeBtn);
        refreshLayout = findViewById(R.id.main_refreshLayout);

        addBtn.setOnClickListener(this);
        removeBtn.setOnClickListener(this);

        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new RecyclerViewBean("标题", "内容", getRandom()));
        }
        adapter = new RecyclerViewAdapter(this, list, new RecyclerViewAdapter.onItemViewClickListener() {
            @Override
            public void onItemClick(int position) {
                list.get(position).setTitle("点击item更改过的标题");
                adapter.notifyItemChanged(position);
            }
        });
//        LinearLayoutManager manager= new LinearLayoutManager(this);//线性布局
//        GridLayoutManager manager= new GridLayoutManager(this, 4);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
//        manager.setOrientation(RecyclerView.HORIZONTAL); //横向布局 同时将item的宽度设为wrap
        rv.setLayoutManager(manager);
        rv.setOverScrollMode(View.OVER_SCROLL_NEVER); //去掉默认水波纹/回弹动画

        int divider = 20;
        decoration = new androidx.recyclerview.widget.RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull androidx.recyclerview.widget.RecyclerView parent, @NonNull androidx.recyclerview.widget.RecyclerView.State state) {
                outRect.bottom = divider;

                StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                int spanIndex = layoutParams.getSpanIndex();
                if (spanIndex % 2 == 0) { //左边距
                    outRect.left = divider;
                    outRect.right = divider / 2;
                } else { //右边距
                    outRect.left = divider / 2;
                    outRect.right = divider;
                }
            }
        };

//        DividerItemDecoration decoration= new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        rv.addItemDecoration(decoration);
        //增加和移除的动画及持续时间
        DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(500);
        itemAnimator.setRemoveDuration(500);
        rv.setItemAnimator(itemAnimator);
        rv.setAdapter(adapter);

        refreshLayout.setEnableLoadMore(false); //是否开启上拉加载
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                for (int i = 0; i < 5; i++) {
                    list.add(new RecyclerViewBean("1", "2"));
                }
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                refreshLayout.finishRefresh();
            }
        });
    }

    public int getRandom() {
        int[] numbers = {200, 300, 400};
        Random random = new Random();
        int index = random.nextInt(numbers.length);
        return numbers[index];
    }

    /**
     * 点击监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_addBtn:
                list.add(1, new RecyclerViewBean("新标题", "新内容", getRandom()));
                if (adapter != null) {
//                    adapter.notifyItemInserted(1);
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.main_removeBtn:
                list.remove(1);
                if (adapter != null) {
//                    adapter.notifyItemRemoved(1);
                    adapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }
}